var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/express/component/textareaEditor/index.tsx
var textareaEditor_exports = {};
__export(textareaEditor_exports, {
  default: () => textareaEditor_default
});
module.exports = __toCommonJS(textareaEditor_exports);
var import_ahooks = require("ahooks");
var import_antd = require("antd");
var import_react = require("react");
var import_react_dom = require("react-dom");
var import_utils = require("./utils");
var TextareaEditor = (0, import_react.forwardRef)((_, ref) => {
  const { value, setValue, back, forward, reset } = (0, import_ahooks.useHistoryTravel)("");
  const textAreaRef = (0, import_react.useRef)();
  const cursorRef = (0, import_react.useRef)({
    start: 0,
    end: 0
  });
  const handleRecordCursorPosition = () => {
    const position = (0, import_utils.getCursorPosition)(textAreaRef.current);
    cursorRef.current = position;
  };
  const handleOnChange = (e) => {
    setValue(e.target.value);
    handleRecordCursorPosition();
  };
  const insertEdit = (0, import_ahooks.useMemoizedFn)(({ text, type }) => {
    let newText = "";
    const insertStart = cursorRef.current.start;
    const insertEnd = cursorRef.current.end + text.length + 1;
    if (type === "field") {
      newText = `${(value == null ? void 0 : value.slice(0, insertStart)) ?? ""}${text} ${(value == null ? void 0 : value.slice(insertStart)) ?? ""}`;
    } else {
      newText = `${(value == null ? void 0 : value.slice(0, insertStart)) ?? ""}${text}()${(value == null ? void 0 : value.slice(insertStart)) ?? ""}`;
    }
    setValue(newText);
    setTimeout(() => (0, import_react_dom.flushSync)(() => (0, import_utils.setCursorPosition)(textAreaRef.current, insertEnd, insertEnd)), 50);
  });
  const getHtmlValue = () => {
    return value ?? "";
  };
  const setDefaultHtml = (str) => {
    setValue(str ?? "");
  };
  (0, import_react.useImperativeHandle)(ref, () => ({
    insertEdit,
    getHtmlValue,
    setDefaultHtml,
    back,
    forward,
    reset
  }));
  return /* @__PURE__ */ React.createElement(
    import_antd.Input.TextArea,
    {
      style: { height: "100%" },
      ref: textAreaRef,
      placeholder: "请输入文本/插入函数或变量...",
      value,
      onChange: handleOnChange,
      onBlur: handleRecordCursorPosition
    }
  );
});
var textareaEditor_default = TextareaEditor;
